package org.example.springsecurity0317.Service.impl;

import org.example.springsecurity0317.domain.MyUserDetails;
import org.example.springsecurity0317.domain.User;
import org.example.springsecurity0317.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;


@Service
public class UserServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        User user = userMapper.findByName(username);

        User user = userMapper.findUserByName(username);
        MyUserDetails myUserDetails = new MyUserDetails();
        if(user==null){
            throw new RuntimeException("error");
        }
        myUserDetails.setUser(user);
        //brac权限模型
        /*ArrayList<String> permissions=new ArrayList<>();
        permissions.add("admin");
        permissions.add("test");
        myUserDetails.setPermissions(permissions);*/

        return myUserDetails;
    }
}
